/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package remote;

import cbaServer.BankServerRemoteInterface;
import commonSources.Cliente;
import commonSources.Conta;
import commonSources.Funcionario;
import commonSources.Painel;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.swing.UIManager;

/**
 *
 * @author Rente
 */
public class ModuloRemoto {

    private BankServerRemoteInterface bankServerRemoteIf;

    public ModuloRemoto() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Não foi possível alterar a L&F");
        }
        criaLigacaoRMI();

        ModuloRemotoUI mrui = new ModuloRemotoUI(this);
        mrui.setVisible(true);
    }

    public static void main(String[] args) {
        ModuloRemoto remote = new ModuloRemoto();
    }

    private void criaLigacaoRMI() {
        try {
            String rmiAddress = "rmi://localhost/bankserverRemote";
            bankServerRemoteIf = (BankServerRemoteInterface) Naming.lookup(rmiAddress);
            System.out.println("## objecto remoto criado ##");
           // System.out.println(getBankServerIf().sayMyNameBitch());
        } catch (Exception e) {
            System.out.println("## Servidor não atingível ##");
        }
    }

    public Hashtable<String, Cliente> getClientes() {
        try {
            return bankServerRemoteIf.getListaClientes();
        } catch (RemoteException ex) {
           ex.printStackTrace();
            return null;
        }
    }

    public void eliminaCliente(String id) {
        try {
            this.bankServerRemoteIf.removeCliente(id);
        } catch (RemoteException ex) {
            // Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Cliente getCliente(String id) {
        try {
            return this.bankServerRemoteIf.getCliente(id);
        } catch (RemoteException ex) {
            // Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void adicionaCliente(Cliente c) {
        try {
            this.bankServerRemoteIf.adicionaCliente(c);
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Conta getConta(String numConta) {
        try {
            return this.bankServerRemoteIf.getConta(numConta);
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public synchronized void eliminaConta(String numConta, String idCliente) {
        try {
            this.bankServerRemoteIf.eliminaConta(numConta, idCliente);
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public synchronized void novaContaCliente(String idCliente, Conta c) {
        try {
            this.bankServerRemoteIf.novaContaCliente(idCliente, c);
        } catch (RemoteException ex) {
            // Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void adicionaPainel(Painel p) {
        try {
            this.bankServerRemoteIf.adicionaPainel(p);
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void removePainel(String id) {
        try {
            this.bankServerRemoteIf.removePainel(id);
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void associaPainelCliente(String idCliente, String idPainel) {
        try {
            this.bankServerRemoteIf.associaPainelCliente(idCliente, idPainel);
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Hashtable<String, Painel> getPaineis() {
        try {
            return this.bankServerRemoteIf.getListaPaneis();
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    /**
     * @return the bankServerIf
     */
    public BankServerRemoteInterface getBankServerIf() {
        return bankServerRemoteIf;
    }

    Hashtable<String, Funcionario> getFuncs() {
         try {
            return bankServerRemoteIf.getListaFuncs();
        } catch (RemoteException ex) {
           ex.printStackTrace();
            return null;
        }
    }

    void eliminaFunc(String id) {
        try {
            this.bankServerRemoteIf.removeFuncionario(id);
        } catch (RemoteException ex) {
            // Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void adicionaFuncionario(Funcionario f) {
         try {
            this.bankServerRemoteIf.adicionaFuncionario(f);
        } catch (RemoteException ex) {
            //Logger.getLogger(ModuloRemoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
